package cn.com.dcsgo.controller;

import cn.com.dcsgo.model.acl.Role;
import cn.com.dcsgo.result.Result;
import cn.com.dcsgo.service.RoleService;
import cn.com.dcsgo.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/*
接口请求路径、请求方式、请求参数参考前端文件：
uu-preferred-front-end/uuyx-admin/src/api/acl/role.js
 */

/**
 * 角色接口
 *
 * @author Dcsgo
 * @since 2025/3/18 14:01
 */
@Api(tags = "角色接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("admin/acl/role")
public class RoleController {
    private final RoleService roleService;

    /**
     * 角色条件分页查询
     *
     * @param current     页码
     * @param limit       每页记录数
     * @param roleQueryVo 角色查询实体
     * @return Result->IPage->Role
     */
    @ApiOperation("角色条件分页查询")
    @GetMapping("{current}/{limit}")
    public Result<IPage<Role>> getPageList(@PathVariable Long current,
                                           @PathVariable Integer limit,
                                           RoleQueryVo roleQueryVo) {
        //1 创建page对象，传递当前页和每页记录数
        //current 当前页，limit 每页记录数
        Page<Role> pageParam = new Page<>(current, limit);
        IPage<Role> pageResult = roleService.selectRolePage(pageParam, roleQueryVo);
        return Result.ok(pageResult);
    }

    /**
     * 根据id查询角色
     *
     * @param id id
     * @return Result->Role
     */
    @ApiOperation("根据id查询角色")
    @GetMapping("get/{id}")
    public Result<Role> getById(@PathVariable Long id) {
        return Result.ok(roleService.getById(id));
    }

    /**
     * 添加角色
     *
     * @param role role
     * @return Result->null
     */
    @PostMapping("save")
    @ApiOperation("添加角色")
    public Result<Serializable> save(@RequestBody Role role) {
        if (roleService.save(role)) {
            return Result.ok(null);
        }
        return Result.fail(null);
    }

    /**
     * 修改角色信息
     *
     * @param role role
     * @return Result->null
     */
    @PutMapping("update")
    @ApiOperation("根据id更新角色")
    public Result<Serializable> updateById(@RequestBody Role role) {
        if (roleService.updateById(role)) {
            return Result.ok(null);
        }
        return Result.fail(null);
    }

    /**
     * 根据id删除角色
     *
     * @param id id
     * @return Result->null
     */
    @ApiOperation("根据id删除角色")
    @DeleteMapping("remove/{id}")
    public Result<Serializable> remove(@PathVariable Long id) {
        if (roleService.removeById(id)) {
            return Result.ok(null);
        }
        return Result.fail(null);
    }

    /**
     * 批量删除角色
     *
     * @param ids ids
     * @return Result->null
     */
    @ApiOperation("批量删除角色")
    @DeleteMapping("batchRemove")
    public Result<Serializable> batchRemove(@RequestBody List<Long> ids) {
        if (roleService.removeByIds(ids)) {
            return Result.ok(null);
        }
        return Result.fail(null);
    }
}
